<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="560px"
    append-to-body
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <el-form :model="info" :rules="rules" ref="queryForm" label-width="120px" size="small" class="pr40">
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="info.name" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="规范类型" prop="ruleId">
        <el-select v-model="info.ruleId" placeholder="" clearable style="width: 100%">
          <el-option v-for="item in ruleList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="模板类型" prop="languageType">
        <el-select v-model="info.languageType" placeholder="" clearable style="width: 100%">
          <el-option label="中文" value="zh" />
          <el-option label="中英文" value="both" />
        </el-select>
      </el-form-item>
      <el-form-item label="适用类型" prop="classifyId">
        <el-select v-model="info.classifyId" disabled style="width: 100%">
          <el-option :label="classifyObj ? classifyObj.name : ''" :value="info.classifyId" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer pr40">
      <el-button size="small" @click="open = false">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit" size="small">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRulesList, handleTemplateInfo } from '@/api/business'
import { Message } from 'element-ui'
import dayjs from 'dayjs'
import { getClassifyObj } from '@/utils/index'

export default {
  name: 'TemplateEditModal',
  inject: ['getTreeData'],
  computed: {
    treeData() {
      return this.getTreeData()
    }
  },
  props: {
    editData: {
      type: Object,
      default: () => ({})
    },
    visible: {
      type: Boolean,
      default: false
    },
    selectedNode: {
      type: Object,
      default: () => ({})
    },
    currentUser: {
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    visible(val) {
      this.open = val
      if (val) {
        this.now = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
        this.$nextTick(() => {
          this.$refs.queryForm.clearValidate();
          this.title = this.editData?.id ? '编辑模板' : '新增模板'
        })
      }
    },
    editData(val) {
      this.info = JSON.parse(JSON.stringify(val))
      this.info.classifyId = this.info.classifyId || this.selectedNode.id
      if (this.selectedNode?.id) {
        this.classifyObj = this.selectedNode
      } else {
        this.classifyObj = getClassifyObj(this.treeData, this.info.classifyId)
      }
    }
  },
  data() {
    return {
      open: this.visible,
      info: this.editData,
      title: '新增模板',
      loading: false,
      ruleList: [],
      rules: {
        name: [
          { required: true, message: '请输入模板名称', trigger: 'change' },
          { pattern: /\s*\S+?/, message: '请输入非空字符', trigger: 'change' }
        ],
        classifyId: [{ required: true, message: "请选择文件分类", trigger: "change" }],
        ruleId: [{ required: true, message: '请选择规范类型', trigger: 'change' }],
        languageType: [{ required: true, message: '请选择模板类型', trigger: 'change' }],
      },
      classifyObj: {},
      now: null
    }
  },
  mounted() {
    this.getRules()
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false)
      this.$emit('onClose')
    },
    handleSubmit() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.info))
          this.loading = true
          handleTemplateInfo(params, this.editData?.name ? 'put' : 'post')
            .then(() => {
              this.open = false
              Message.success('保存成功')
              this.$emit('onSuccess')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    getRules() {
      getRulesList()
        .then((res) => {
          this.ruleList = res.data
        })
        .catch(() => {
          Message.error('规范查询失败')
        })
    },
  }
}
</script>
<style scoped>
.dialog,
.el-select-dropdown__item {
  font-family: 'microsoft yahei';
}
</style>
